import { createSlice } from '@reduxjs/toolkit';
import type { TimerState } from '../../types/timer';

const initialState: TimerState = {
  timeLeft: 15,
  isActive: false,
};

export const timerSlice = createSlice({
  name: 'timer',
  initialState,
  reducers: {
    startTimer(state) {
      state.isActive = true;
    },
    stopTimer(state) {
      state.isActive = false;
    },
    decrementTimer(state) {
      if (state.isActive && state.timeLeft > 0) {
        state.timeLeft -= 1;
      }
    },
    resetTimer(state) {
      state.timeLeft = 15;
      state.isActive = false;
    },
  },
});

export const { startTimer, stopTimer, decrementTimer, resetTimer } = timerSlice.actions;

export default timerSlice.reducer;
